/**
 * @(#)ChannelApkInfoService.java 2016年11月11日
 * Copyright(C) 2016 ZHEXIN IT CO.,LTD. All rights reserved.
 */
package org.jsbd.boss.service.impl;

import java.io.File;
import java.util.Date;
import java.util.Map;

import org.apache.commons.io.FileUtils;
import org.apache.commons.lang.StringUtils;
import org.jsbd.boss.dao.IDaemonApkConfigDAO;
import org.jsbd.boss.domian.channel.DaemonApkConfig;
import org.jsbd.boss.domian.channel.FileUploadVo;
import org.jsbd.boss.util.DateUtil;
import org.jsbd.boss.util.OssUtils;
import org.jsbd.boss.util.PropertiesUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import edu.hziee.cap.common.util.Md5Utils;

/**
 * 
 * 母包apk管理类
 * @author <a href="mailto:zhangbinghua@zhexinit.com" >zbh</a>
 * @version 1.0.0
 */
@Service
public class DaemonApkInfoService  {
	public static final Logger LOGGER = LoggerFactory.getLogger(DaemonApkInfoService.class);
	
    @Autowired
    private IDaemonApkConfigDAO daemonApkConfigDAO;
    @Transactional("transactionManagerCrack")
	public Integer saveApkConfig(MultipartFile[] files, DaemonApkConfig apkConfig) {
		if (files != null && files.length > 0) {
				MultipartFile file = files[0];
				FileUploadVo fileVo = uploadFile(file);
				if (fileVo != null) {
					String fileName = file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf("/") + 1);
					String fileType = fileName.substring(fileName.lastIndexOf(".") + 1);
					String filePath = fileVo.getFilePath();
					String md5 = fileVo.getMd5();
					if (StringUtils.isNotBlank(fileType)) {
							apkConfig.setMark(md5);
							apkConfig.setFilePath(filePath);
						}
					}
				}
		return daemonApkConfigDAO.insertConfig(apkConfig);
	}
	/**
	 * 上传文件
	 * 
	 * @return
	 */
	public FileUploadVo uploadFile(MultipartFile file) {
		String fileUploadPath = PropertiesUtil.getValue("game_file_upload_path");
		FileUploadVo fileVo = null;
		if (file != null && !file.isEmpty()) {
			File apkFile = null;
			try {
				fileVo = new FileUploadVo();
				String originalFilename = file.getOriginalFilename();
				String fileName = originalFilename.substring(originalFilename.lastIndexOf("/") + 1);
				String fileType = fileName.substring(fileName.lastIndexOf(".") + 1);
				String fileSaveName = new Date().getTime() + fileName;
				apkFile = new File(fileUploadPath, fileSaveName);
				// 这里不必处理IO流关闭的问题，因为FileUtils.copyInputStreamToFile()方法内部会自动把用到的IO流关掉
				FileUtils.copyInputStreamToFile(file.getInputStream(), apkFile);
				String md5 = Md5Utils.md5Hex(FileUtils.readFileToByteArray(apkFile));
				String day = DateUtil.getCurrentDateStr();
				fileName = "daemonapk/"+day + "/" + md5;
				OssUtils.uploadFile(fileName, file.getInputStream());
				fileVo.setMd5(md5);
				fileVo.setFilePath(fileName);
			} catch (Exception e) {
				LOGGER.error(e.getMessage(), e);
			} finally {
				if (apkFile != null) {
					apkFile.setWritable(true);
					apkFile.delete();
				}
			}
		}
		return fileVo;
	}
	
	public Integer updateApkConfig(String mark,String isDelete){
		DaemonApkConfig apkConfig = new DaemonApkConfig();
		apkConfig.setMark(mark);
		apkConfig.setIsDelete(isDelete);
		return daemonApkConfigDAO.updateConfig(apkConfig);
	}
	
	public Map<String, Object> getApkConfigByPage(DaemonApkConfig mark){
		return daemonApkConfigDAO.selectConfigPageListMap(mark);
	}
	
	public DaemonApkConfig selectVer(String version){
		return daemonApkConfigDAO.selectVer(version);
	}
	public void phoneTypeEdit(DaemonApkConfig daemonApkConfig) {
		daemonApkConfigDAO.phoneTypeEdit(daemonApkConfig);
	}
	public DaemonApkConfig getByVersion(String version) {
		return daemonApkConfigDAO.selectByVersion(version);
	}
}